
// pages/index/details.js
const app = getApp(), util = require('../../utils/util.js');
let _this;
let pageNo = 1, pages = '', limit = 10;
let nomber = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //地址
    appLink: app.globalData.appLink, //app.globalData.appLink,
    // 链接判断
    buttonClicked: false,
    // 默认简介
    tabContMas:0, 
    likeRepeat: true,
    joinUserNo: 8,
    location: {
      address: '重庆市渝北区恒大中渝广场3号楼1104室',
      latitude: 29.59658148437869,
      longitude: 106.52197800000002
    },
    showPost: false,
    downloadingFile: false,
    markers: [{
      iconPath: '/dist/images/location.png',
      id: 0,
      zIndex: -1,
      latitude: 29.59658148437869,
      longitude: 106.52197800000002,
      width: 30,
      height: 30,
      callout: {
        content: '重庆市渝北区恒大中渝广场3号楼1104室',
        anchorX: 0,
        anchorY: 0,
        color: '#fff',
        bgColor: '#00ccbb',
        borderRadius: 4,
        padding: 10,
        display: 'ALWAYS'
      }
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
    _this.init(options.salonId);
    _this.setData({ salonId: options.salonId});
  },
  init: function (salonId){
    _this.setData({
      userInfo: app.globalData.programUserInfo
    })
    if (salonId){
      _this.getDetails(salonId);
      _this.joinUser(salonId)
    }
  },
  // 获取详情
  getDetails: function (id) {
    let data={}, url = 'listDetails';
    data.salonId = id;
    app.getMassage(data, null, url, function (res) {
      if (res.data) {
        // 判断是否过期
        let newTime = new Date(),
          joinEndTime = new Date(res.data.joinEndTime.replace(/-/g, '/'));
        _this.setData({
          details: res.data,
          expired: joinEndTime.getTime() < newTime.getTime() ? true : false
        })
        // 刷新上一頁
        _this.breakPage();
      } else {
        util.toastMsg('请稍后再试', 'none');
      }
    },'GET')
  },
  // nav 切换
  tabCont: function (e) {
    var title = e.currentTarget.dataset.title;
    if (nomber == 0 && title == 1){
      _this.list(1,10); 
    }
    _this.setData({
      tabContMas: title
    })
  },
  // 获取列表
  list: function (page, lim) {
    var data = {}, url = "salonObject";
    data.typeId = _this.data.details.salonType.id;
    app.getList(page, lim, data, url, function (res) {
      if (res.data) {
        if (page == 1) {
          _this.setData({
            newList: [],
          })
        }
        pageNo = res.data.page / 1 + 1;
        pages = res.data.pages;
        var list = res.data.list;
        for(var i=0; i<list.length; i++){
          list[i].salonBeginTime = list[i].salonBeginTime.split(' ')[0];
        }
        var newList = _this.data.newList;
        newList = newList.concat(list);
        _this.setData({
          newList: newList,
          pageNo: pageNo,
          pages: pages
        })
      }
    })
  },
  // 滚动触底
  scrolltolower: function () {
    // 判断是否是最后一页
    if (pages >= pageNo) {
      _this.list(pageNo, limit);
    }
  },
  // 报名
  signUp: function (e) {
    wx.showLoading({
      title: '正在加入沙龙...',
    })
    let data = {};
    data.salonId = _this.data.details.id;
    data.userId = _this.data.userInfo.id;
    app.getMassage(data, null, 'signUp', function(res){
      console.log(res);
      wx.hideLoading();
      if (res.status == 'SUCCESS'){
        util.toastMsg('加入沙龙成功','success');
        _this.init(_this.data.salonId);
      }else{
        util.toastMsg(res.message,'none');
      }
    }, 'GET');
  },
  // 点赞
  like:function(){
    if (!_this.data.likeRepeat){
      util.toastMsg('请勿重复提交', 'success');
      return false;
    }
    wx.showLoading({
      title: '正在点赞...',
    })
    _this.setData({
      likeRepeat: false
    })
    let data = {};
    data.salonId = _this.data.details.id;
    data.userId = _this.data.userInfo.id;
    console.log('点赞');
    app.getMassage(data, null, 'like', function (res) {
      console.log(res);
      wx.hideLoading();
      _this.setData({
        likeRepeat: true
      })
      if (res.status == 'SUCCESS') {
        util.toastMsg('点赞成功', 'success');
        _this.init(_this.data.salonId);
      } else {
        util.toastMsg(res.message, 'none');
      }
    });
  },
  // 加入沙龙的人
  // 点赞
  joinUser: function (salonId) {
    let data = {};
    data.salonId = salonId;
    app.getMassage(data, null, 'joinUser', function (res) {
      if (res.status == 'SUCCESS') {
        _this.setData({ joinUser: res.data})
      } else {
        //util.toastMsg(res.message, 'none');
      }
    },'GET');
  },
  joinUserNoFun: function () {
    let joinUser = _this.data.joinUser,
      joinUserNo = _this.data.joinUserNo;
      _this.setData({
        joinUserNo: joinUserNo == 8 ? joinUserNo = joinUser.length : joinUser = 8
      })
  },
  breakPage:function(){
    let page = getCurrentPages(),
      indexPage = page[0];
    indexPage.init();
    if (page.length > 2) {
      let prevPage = page[page.length - 1];
      prevPage.init();
    }
  },
  // 去**页
  goNavTo: function (e) {
    app.goNavTo(e, _this.data.buttonClicked, _this);
  },
  // 返回上一页
  goBack: function (e) {
    app.goBack(e, _this.data.buttonClicked, _this);
  },

  openLocation() {
    wx.openLocation(this.data.location)
  },

  showPost() {
    _this.setData({
      showPost: true
    })
  },

  hidePost() {
    _this.setData({
      showPost: false
    })
  },

  savePost() {
    _this.setData({
      downloadingFile: true
    })
    wx.downloadFile({
      url: this.data.appLink + '/salonObject/drawShareImage?salonId=' + this.data.salonId,
      success: function(res) {
        if (res.statusCode === 200) {
          let img = res.tempFilePath;
          wx.saveImageToPhotosAlbum({
            filePath: img,
            success(res) {
              wx.showToast({
                title: '保存成功',
                icon: 'success'
              });
            },
            fail(res) {
              wx.showToast({
                title: '保存失败',
                icon: 'success'
              });
            },
            complete() {
              _this.setData({
                downloadingFile: false
              })
            }
          });
        }
      }
    });
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})